package com.zmc.admin.controller.system;

import com.zmc.admin.infrastructure.bean.vo.notice.SysNoticeVo;
import com.zmc.admin.infrastructure.entity.SysNoticeEntity;
import com.zmc.admin.infrastructure.bean.query.SysNoticeQuery;
import com.zmc.admin.infrastructure.service.SysNoticeService;

import com.zmc.common.annotation.PreventRepeatSubmit;
import com.zmc.common.enums.RequestSourceEnum;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.R;
import com.zmc.common.utils.token.AuthTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 公告 信息操作处理
 * @author zmc
 */
@RestController
@RequestMapping("/notice")
public class SysNoticeController {
    @Autowired
    private SysNoticeService noticeService;

    @Autowired
    private AuthTokenService authTokenService;

    /**
     * 获取通知公告列表
     */
    @PostMapping("/list")
    public R list(@RequestBody SysNoticeQuery query) {
        PageUtils page = noticeService.selectNoticeList(query);
        return R.page(page);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @PostMapping(value = "/info")
    public R getInfo(@RequestBody SysNoticeVo vo) {
        return R.ok(noticeService.getById(vo.getNoticeId()));
    }

    /**
     * 新增通知公告
     */
    @PostMapping("/add")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R add(@RequestBody SysNoticeEntity notice) {
        notice.setCreateBy(authTokenService.getUserInfo().getUsername());
        noticeService.save(notice);
        return R.ok();
    }

    /**
     * 修改通知公告
     */
    @PostMapping("/update")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R edit(@Validated @RequestBody SysNoticeEntity notice) {
        notice.setUpdateBy(authTokenService.getUserInfo().getUsername());
        noticeService.updateById(notice);
        return R.ok();
    }

    /**
     * 删除通知公告
     */
    @PostMapping("/delete")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R remove(@RequestBody SysNoticeVo vo) {
        noticeService.removeById(vo.getNoticeId());
        return R.ok();
    }
}
